package com.ars.listStatistics;

import com.ars.db.DBTools;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Turnover {

    //根据网点和月份获取营业额
    public double getTurnover(int wid, int year, int month) throws SQLException {
        double turnover = 0;
        String sql = "SELECT oid from orders WHERE wid = " + wid + getDateCondition(year, month);
        ResultSet rs = new DBTools().query(sql);
        while (rs.next()) {
            turnover += new Order(rs.getInt("oid")).getPrice();
        }
        return turnover;
    }

    private String getDateCondition(int year, int month) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar date = Calendar.getInstance();
        //注意空格和单引号
        String dateCondition = " and date >='";

        //注意Calendar中月份从0开始算起，11实际是12月
        date.set(year, month - 1, 1);
        dateCondition += format.format(date.getTime()) + "' and date < '";
        date.set(year, month, 1);
        dateCondition += format.format(date.getTime()) + "'";
        return dateCondition;
    }
}
